package com.hsgene.consumables.controller.peanut;

import com.hsgene.common.util.exception.CommonErrorCode;
import com.hsgene.common.util.jwt.IJWTInfo;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.consumables.dto.v1.GoodsApplyOperationDto;
import com.hsgene.consumables.dto.v1.operation.GoodsApplyModifyDto;
import com.hsgene.consumables.service.GoodsApplyService;
import com.hsgene.consumables.utils.JwtToken;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 地服耗材申请Controller
 *
 * @author wangbing
 * @version 1.0, 2018/9/17
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/goods/applys")
public class GoodsApplyController {

    private final static Logger LOGGER = LoggerFactory.getLogger(GoodsApplyController.class);

    @Resource
    private GoodsApplyService goodsApplyService;

    @Resource
    private JwtToken jwtToken;

    /**
     * 耗材申请操作API，根据actionCode区分操作类型
     *
     * @param operationInfo 耗材申请操作详情
     * @return 操作结果
     */
    @PostMapping("")
    public ApiResult<NullObject> goodsApply(
            @RequestBody GoodsApplyOperationDto operationInfo,
            HttpServletRequest request) {
        String groundServantId;
        try {
            String authorization = request.getHeader("Authorization");
            if (StringUtils.isBlank(authorization)) {
                authorization = request.getHeader("authorization");
            }
            IJWTInfo jwtInfo = jwtToken.getInfoFromToken(authorization);
            groundServantId = jwtInfo.getId();
        } catch (Exception e) {
            LOGGER.error("Authorization-Token解析异常：", e);
            return ApiResult.fail(CommonErrorCode.AUTHORIZATION_PARSE_EXCEPTION);
        }
        return goodsApplyService.goodsApplyOperation(groundServantId, operationInfo);
    }

    /**
     * 修改耗材申请
     *
     * @param goodsApplyModifyDto
     * @return
     */
    @PutMapping("update/{apply_id}")
    public ApiResult<NullObject> updateGoodsApply(@PathVariable("apply_id")String id,@RequestBody GoodsApplyModifyDto goodsApplyModifyDto) {
        goodsApplyModifyDto.setApply_id(id);
        return goodsApplyService.updateGoodsApply(goodsApplyModifyDto);
    }


}
